package com.raysdata.smartcity.dao.sys;

import com.raysdata.smartcity.HelperClazz;
import com.raysdata.smartcity.dao.RzBaseDao;
import com.raysdata.smartcity.dto.Log;
import com.raysdata.smartcity.dto.User;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.UUID;

/**
 * Created by bh on 2016/12/6.
 */
@Component
public class LogDao {
    @Autowired
    private RzBaseDao rzBaseDao;

    public int saveLog(HttpServletRequest request,String content, String type, User user, String object, String status){
        Log log=new Log();

        String ipString = request.getHeader("x-forwarded-for");
        if (StringUtils.isBlank(ipString) || "unknown".equalsIgnoreCase(ipString)) {
            ipString = request.getHeader("Proxy-Client-IP");
        }
        if (StringUtils.isBlank(ipString) || "unknown".equalsIgnoreCase(ipString)) {
            ipString = request.getHeader("WL-Proxy-Client-IP");
        }
        if (StringUtils.isBlank(ipString) || "unknown".equalsIgnoreCase(ipString)) {
            ipString = request.getRemoteAddr();
        }

        // 多个路由时，取第一个非unknown的ip
        final String[] arr = ipString.split(",");
        for (final String str : arr) {
            if (!"unknown".equalsIgnoreCase(str)) {
                ipString = str;
                break;
            }
        }

        log.setContent(user.getUser_name()+" "+content+" ("+status+")");
        log.setLog_type(type);
        log.setCperson(user.getLogin_name());
        log.setIP(ipString);
        log.setObject(object);

        StringBuilder sql = new StringBuilder();
        sql.append("INSERT INTO `ss_log` ( ");
        sql.append("`log_id`, ");
        sql.append("`content`, ");
        sql.append("`log_type`, ");
        sql.append("`cperson`, ");
        sql.append("`cdate`, ");
        sql.append("`object`, ");
        sql.append("`IP`) ");

        sql.append(" VALUES ");
        sql.append("('" + UUID.randomUUID().toString().trim().replaceAll("-", "") + "',");
        sql.append(":content, ");
        sql.append(":log_type, ");
        sql.append(":cperson, ");
        sql.append("now(), ");
        sql.append(":object, ");
        sql.append(":IP)");
        rzBaseDao.insert(sql.toString(), log);
        return 0;
    }

    public List<Log> queryLog(Integer page, Integer count, Log log) {
        StringBuilder sb=new StringBuilder();
        sb.append("select * from ss_log l where 1=1 ");
        if (!HelperClazz.isEmpty(log.getContent())) {
            sb.append(" and l.content like '%" + log.getContent()+ "%' ");
        }

        sb.append(" ORDER BY l.cdate desc");
        Object[] params = new Object[0];
        int index = 0;
        Object[] paramsEnd = new Object[index];
        System.arraycopy(params, 0, paramsEnd, 0, index);
        if (page <= 0 || count <= 0) {//all
            return rzBaseDao.query(sb.toString(), Log.class, paramsEnd);
        } else {
            count = (count > 0) ? count : Integer.MAX_VALUE;
            return rzBaseDao.queryBySlice(sb.toString(), Log.class, paramsEnd, page, count);
        }
    }
}
